package poradnik.medyczny.Model;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import poradnik.medyczny.DB.DBAdapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class Symptom {
	long id;
	boolean isChecked;
	private boolean isAsked = false;
	ArrayList<Likehood> disease = new ArrayList<Likehood>();
	ArrayList<Likehood> localization = new ArrayList<Likehood>();
	String name;
	String question;

	public Symptom(long id, Context context) {
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();
		Cursor cursor = medical_db.getSymptom(id);
		if (cursor != null)
			if (cursor.moveToFirst()) {
				question = cursor.getString(cursor.getColumnIndex(DBAdapter.SYMPTOMS_QUESTION));
				this.id = cursor.getLong(cursor.getColumnIndex(DBAdapter.SYMPTOMS_ID));
				name = cursor.getString(cursor.getColumnIndex(DBAdapter.SYMPTOMS_NAME));
				Cursor diseaseCursor = medical_db.getSymptomLocalizationBySym(id);
				if (diseaseCursor != null) {
					while (diseaseCursor.moveToNext()) {
						Likehood likehood = new Likehood();
						likehood.symptomId = id;
						likehood.likehood = diseaseCursor.getInt(diseaseCursor.getColumnIndex(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_LIKEHOOD));
						likehood.localiztionId = diseaseCursor.getLong(diseaseCursor.getColumnIndex(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_LOCALIZATION_ID));
						likehood.id = diseaseCursor.getLong(diseaseCursor.getColumnIndex(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_ID));
						localization.add(likehood);
					}
					diseaseCursor.close();
				}
				Cursor symptomCursor = medical_db.getSymptomDiseaseLikehoodBySym(id);
				if (symptomCursor != null) {
					while (symptomCursor.moveToNext()) {
						Likehood likehood = new Likehood();
						likehood.symptomId = id;
						likehood.likehood = symptomCursor.getInt(symptomCursor.getColumnIndex(DBAdapter.SYMPTOM_DISEASE_LIKEHOODS_LIKEHOOD));
						likehood.diseaseId = symptomCursor.getLong(symptomCursor.getColumnIndex(DBAdapter.SYMPTOM_DISEASE_LIKEHOODS_DISEASE_ID));
						likehood.id = symptomCursor.getLong(symptomCursor.getColumnIndex(DBAdapter.SYMPTOM_DISEASE_LIKEHOODS_ID));
						disease.add(likehood);
					}
					symptomCursor.close();
				}
			}
		medical_db.close();
	}

	public Symptom() {
	}

	public static Symptom parseJSON(JSONObject jsonObject) throws JSONException {
		Symptom sym = new Symptom();
		sym.id = jsonObject.getLong("id");
		sym.name = jsonObject.getString("name");
		sym.question = jsonObject.getString("question");
		return sym;
	}

	public boolean Save(Context context) {
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();

		ContentValues values = new ContentValues();
		values.put(DBAdapter.SYMPTOMS_NAME, name);
		values.put(DBAdapter.SYMPTOMS_QUESTION, question);

		boolean successful = medical_db.update(DBAdapter.TABLE_SYMPTOMS, values, DBAdapter.SYMPTOMS_ID + "='" + id + "'", null);
		if (!successful) {
			values.put(DBAdapter.SYMPTOMS_ID, id);
			id = medical_db.insert(DBAdapter.TABLE_SYMPTOMS, values);
			successful = id == 0 ? false : true;

		}
		medical_db.close();

		return successful;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public boolean isChecked() {
		return isChecked;
	}

	public void setChecked(boolean isChecked) {
		this.isChecked = isChecked;
	}

	public ArrayList<Likehood> getDisease() {
		return disease;
	}

	public void setDisease(ArrayList<Likehood> disease) {
		this.disease = disease;
	}

	public ArrayList<Likehood> getLocalization() {
		return localization;
	}

	public void setLocalization(ArrayList<Likehood> localization) {
		this.localization = localization;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public static ArrayList<Symptom> LoadNewSymptoms(Disease mostProbableDisease, ArrayList<Symptom> symptomsList, Context context) {
		for (int i = 0; i < mostProbableDisease.getSymptoms().size(); i++) {
			boolean isAlreadyAdded = false;
			for (int l = 0; l < symptomsList.size(); l++) {
				if (symptomsList.get(l).getId() == mostProbableDisease.getSymptoms().get(i).getId())
					isAlreadyAdded = true;
			}
			if (!isAlreadyAdded) {
				symptomsList.add(new Symptom(mostProbableDisease.getSymptoms().get(i).getId(), context));
			}
		}
		return symptomsList;
	}

	public boolean isAsked() {
		return isAsked;
	}

	public void setAsked(boolean isAsked) {
		this.isAsked = isAsked;
	}
}
